package com.wtao.testcamera;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.senseauto.subgroup.autocameralib.CameraFactory;
import com.senseauto.subgroup.autocameralib.callback.CameraImageCallback;
import com.senseauto.subgroup.autocameralib.model.CameraOptions;
import com.senseauto.subgroup.autocommonlib.utilcode.util.ImageUtils;
import com.wtao.testcamera.utils.FileUtils;

public class RecordActivity extends AppCompatActivity implements CameraImageCallback, View.OnClickListener {
    String TAG = "RecordActivity";
    TextureView textureView;
    Button btnRecord;
    int cameraId = 45;
    CameraFactory mCameraFactory;

    boolean isRecording = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        textureView = findViewById(R.id.texture);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                initCamera();
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
        findViewById(R.id.switchCamera).setOnClickListener(this);
        btnRecord = findViewById(R.id.record);
        btnRecord.setOnClickListener(this);
    }

    public void initCamera() {
        CameraOptions cameraOptions = new CameraOptions();
        cameraOptions.setImgWidth(ConstantsGAC.SDK_INPUT_IMG_W);
        cameraOptions.setImgHeight(ConstantsGAC.SDK_INPUT_IMG_H);
        //录制适配需要这种格式
        cameraOptions.setImageFormat(ImageUtils.ImageFormat.YUV420SP);
        //德赛车机A02的cameraId变成了45和49，不再是0和1了
        Log.e("initCamera", "cameraId=" + cameraId);
        cameraOptions.setCameraId(cameraId);
        cameraOptions.setFrameRate(25);
        cameraOptions.setRecordMP4Path(FileUtils.getVideoFilePath(this));
        textureView.setScaleX(-1f);

        if (mCameraFactory == null) {
            mCameraFactory = CameraFactory.getInstance().createCameraV2Proxy(this, cameraOptions,
                    RecordActivity.this,
                    new Surface(textureView.getSurfaceTexture()));
        }

        mCameraFactory.openCamera();
        Log.e(TAG, "initCamera() mCameraFactory.openCamera()");
    }

    //----start
    @Override
    public void onReadImageData(@Nullable byte[] bytes, int i, int i1) {

    }

    @Override
    public void onException(@NonNull Exception e) {

    }
    //----end
    @Override
    public void onResume() {
        super.onResume();
        if (mCameraFactory != null) {
            Log.e(TAG, "onResume mCameraFactory.openCamera()");
            mCameraFactory.openCamera();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mCameraFactory != null) {
            mCameraFactory.releaseCamera();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.switchCamera:
                if (cameraId == ConstantsGAC.CAMERA_ID_DMS) {
                    cameraId = ConstantsGAC.CAMERA_ID_OMS;
                } else if (cameraId == ConstantsGAC.CAMERA_ID_OMS) {
                    cameraId = ConstantsGAC.CAMERA_ID_DMS;
                }
                if (mCameraFactory != null) {
                    mCameraFactory.changeCamera(cameraId);
                }
                break;
            case R.id.record:
                if (!isRecording) { //不在录制，开启录制
                    isRecording = true;
                    String filePath = FileUtils.getVideoFilePath(getApplicationContext());
                    CameraFactory.getInstance().updateRecordVideoFilePath(filePath);
                    Log.e(TAG, "视频将存在："+filePath);
                    //回调拿到的数据会写入文件，最后停止录制会最多保留3个文件
                    CameraFactory.getInstance().startRecordingVideo();
                    btnRecord.setTextColor(getColor(R.color.red));
                    btnRecord.setText(getResources().getText(R.string.text_recording));
                } else {
                    //停止录制
                    isRecording = false;
                    CameraFactory.getInstance().stopRecordingVideo();
                    btnRecord.setTextColor(getColor(R.color.white));
                    btnRecord.setText(getResources().getText(R.string.text_record));
                }
                break;
        }
    }
}